﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using BusLogic;

public partial class OrderDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            load_TypeCheckOut();
            load_RecordStatus();

            if (Request.QueryString["id"] != null)
            {
                int iOrderID = int.Parse(Request.QueryString["id"].ToString());
                get_ORDER(iOrderID);
                get_PRODUCT_BY_ORDER(iOrderID);
            }            
        }
    }

    public void get_ORDER(int iOrderID)
    {
        DataTable dtOrder = Order.get_ORDER(iOrderID).Tables[0];

        if(dtOrder.Rows.Count > 0)
        {
            ltCustomerName.Text = dtOrder.Rows[0]["CUSTOMER_NAME"].ToString();
            ltAddress.Text = dtOrder.Rows[0]["ADDRESS"].ToString();
            ltAddressDelivery.Text = dtOrder.Rows[0]["ADDRESS_DELIVERY"].ToString();
            ltCreateDate.Text = dtOrder.Rows[0]["CREATE_DATE"].ToString();
            ltEmail.Text = dtOrder.Rows[0]["EMAIL"].ToString();
            ltMobile.Text = dtOrder.Rows[0]["MOBILE"].ToString();
            ltMessage.Text = dtOrder.Rows[0]["MESSAGE"].ToString();

            rbCheckOut.SelectedValue = dtOrder.Rows[0]["TYPE_CHECKOUT"].ToString();
            rdlRecordStatus.SelectedValue = dtOrder.Rows[0]["RECORD_STATUS"].ToString();
        }
        
    }

    public void get_PRODUCT_BY_ORDER(int iOrderID)
    {
        DataTable dtProduct = Order.get_PRODUCT_BY_ORDER(iOrderID).Tables[0];

        int iTotalQuantity = 0;
        double iTotalCart = 0;
        for (int i = 0; i < dtProduct.Rows.Count; i++)
        {
            iTotalQuantity = iTotalQuantity + int.Parse(dtProduct.Rows[i]["QUANTITY"].ToString());
            iTotalCart = iTotalCart + (double.Parse(dtProduct.Rows[i]["PRICE"].ToString()) * int.Parse(dtProduct.Rows[i]["QUANTITY"].ToString()));
        }

        rpShoppingCart.DataSource = dtProduct;
        rpShoppingCart.DataBind();

        ltTotalQuantity.Text = iTotalQuantity.ToString();
        ltTotalCart.Text = String.Format("{0:0,0 VNĐ}", iTotalCart);
    }

    protected void lbtSave_Click(object sender, EventArgs e)
    {
        int iOrderID = int.Parse(Request.QueryString["id"].ToString());

        Order.update_ORDER(iOrderID, int.Parse(rbCheckOut.SelectedValue), Boolean.Parse(rdlRecordStatus.SelectedValue));

        Response.Redirect("OrderManager.aspx");
    }

    public void load_RecordStatus()
    {
        ListItem item = new ListItem();

        item.Value = "True";
        item.Text = "Đã duyệt";        
        rdlRecordStatus.Items.Add(item);

        item = new ListItem();
        item.Value = "False";
        item.Text = "Chờ";
        rdlRecordStatus.Items.Add(item);
    }

    public void load_TypeCheckOut()
    {
        ListItem item = new ListItem();

        item.Value = "1";
        item.Text = "Bưu điện";        
        rbCheckOut.Items.Add(item);

        item = new ListItem();
        item.Value = "2";
        item.Text = "Ngân hàng";
        rbCheckOut.Items.Add(item);

        item = new ListItem();
        item.Value = "3";
        item.Text = "Clerm";
        rbCheckOut.Items.Add(item);

        item = new ListItem();
        item.Value = "4";
        item.Text = "Nơi nhận hàng";
        rbCheckOut.Items.Add(item);
    }

    public string get_Image_Product(object data)
    {
        string sImageUrl = data.ToString();

        String[] sArrImage = sImageUrl.Split(new Char[] { ';' });

        return sArrImage[0];
    } 
}
